<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 	
*/
class Coach extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('User/CoachModel','coach');
	}


	//教练列表
	public function index()
	{
		//获取场馆
		$data['venue'] = $this->coach->get_venue_list();

		$this->load->view('coach_list', $data);
	}

	//获取数据
	public function get_list()
	{
		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$term['venue'] = $this->input->get('venue');
		$term['keyword'] = $this->input->get('keyword');

		$res = $this->coach->get_list_data($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//添加、编辑页
	public function coach_info()
	{
		$data['cocah_id'] = intval($this->uri->segment(3));
		
		//编辑
		if ($data['cocah_id'] > 0) 
		{
			$res = $this->coach->get_cocah_row($data['cocah_id']);
		}

		$data['id'] = isset($res['id']) ? $res['id'] : '' ;
		$data['headimg'] = isset($res['headimg']) ? $res['headimg'] : '' ;
		$data['nickname'] = isset($res['nickname']) ? $res['nickname'] : '' ;
		$data['sex'] = isset($res['sex']) ? $res['sex'] : '' ;
		$data['mobile'] = isset($res['mobile']) ? $res['mobile'] : '' ;
		$data['shop_id'] = isset($res['shop_id']) ? $res['shop_id'] : '' ;
		$data['is_sort'] = isset($res['is_sort']) ? $res['is_sort'] : '' ;
		$data['info'] = isset($res['info']) ? $res['info'] : '' ;

		//获取场馆
		$data['venue'] = $this->coach->get_venue_list();

		//图片上传地址
		$data['image_upload_url'] = $this->image_load_url;

		$this->load->view('coach_info', $data);
	}


	//保存入库
	public function insert_add()
	{
		$field = array('headimg','nickname','sex','mobile','shop_id','is_sort','info');		
		$arr = $this->input->post($field, TRUE);
		
		$shop = $this->input->post('shop_id', TRUE);
		if(empty($shop)){
			$data['status'] = 400;
			$data['message'] = '请选择场馆！';
			echo json_encode($data);exit();
		}

		$coach_id = $this->input->post('coach_id');
		//添加
		if (empty($coach_id)) 
		{
			$res = $this->coach->inser_coach_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		//更新
		else
		{
			$res = $this->coach->updata_coach_data($arr,$coach_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}

		}

		echo json_encode($data);exit();
		
	}


	//查看详情
	public function coach_dsec()
	{
		$coach_id = intval($this->uri->segment(3));
		//获取会员基本信息
		$data = $this->coach->coach_count($coach_id); 
		$data['coach_id'] = $coach_id;

		$res = $this->coach->get_cocah_row($coach_id);
		$data['nickname'] = $res['nickname'];
		$data['mobile'] = $res['mobile'];
		$data['headimg'] = $res['headimg'];
		$data['sex'] = $res['sex'];
		$data['info'] = $res['info'];

		$this->load->view('coach_desc',$data);
	}

	//预约曲线
	public function get_appoint()
	{
		$coach_id = $this->input->get('coach_id');
		$date_num = $this->input->get('date_num');

		$res = $this->coach->appoint_data($coach_id,$date_num);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}


	//获取预约记录
	public function get_bespeak()
	{
		$coach_id = intval($this->uri->segment(3));

		$res = $this->coach->bespeak_data($coach_id);

		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}


	//获取会员
	public function get_user()
	{
		$coach_id = intval($this->uri->segment(3));

		$res = $this->coach->user_data($coach_id);

		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();	
	}


	//删除数据
	public function delete_coach()
	{
		$coach_id = $this->input->get('id', true);

		$res = $this->coach->remove_coach($coach_id);

		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}




}


?>